package org.sundatagroup.aurora.monitor.service;

import java.io.IOException;
import java.util.HashSet;

import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.google.gson.JsonObject;

/**
 * <b>监控区前端连接的数据服务</b>
 * <p>存储监控区连接的前端相关的数据
 * </p>
 * 
 * @author Zdream
 * @date 2018-05-28
 * @since 0.2.0
 */
@Service
public class MonitorBatches {
	
	HashSet<WebSocketSession> set = new HashSet<>();

	public void register(WebSocketSession session) {
		set.add(session);
	}

	public void remove(WebSocketSession session) {
		set.remove(session);
	}
	
	public void publishMessage(JsonObject jo) {
		set.forEach((session) -> {
			try {
				synchronized (session) {
					session.sendMessage(new TextMessage(jo.toString()));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
	}
	
}
